<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class baogongFinish extends CI_Controller{
    private $appId;
    private $appSecret;
    
    function __construct() {
        parent::__construct();
        $this->appId = "wx48ac7010d65a20d7";
        $this->appSecret = "yWmfWG4nIkjatVdWuWdgqEk1NhjipzzTW6xHVhODYe2FdE6Q3XkoY2aEy53ShZCi";
        //$this->load->library('baogong');
    }
    public function index($userid) {
        $jsapiTicket = $this->getJsApiTicket();

        // 注意 URL 一定要动态获取，不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        

        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string,
            "userid"    => $userid
        );
        //var_dump($signPackage); 
        $this->load->view("baogong",$signPackage);
    }

  private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
          $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
  }

  private function getJsApiTicket() {
        // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
        //$data = json_decode(file_get_contents("weixinJS/jsapi_ticket.json"));
        $data = $this->Data_model->get_single_data(array('type' => 'jsapi_ticket'),"WEIXININFO");
        if ($data['time'] < time()) {
            $accessToken = $this->get_access_token();
            // 如果是企业号用以下 URL 获取 ticket
            $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
            //$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->http_request_json($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $this->Data_model->update_data(array('type' => 'jsapi_ticket'),array('time' => time() + 7000,'jsapi_ticket' => $ticket),"WEIXININFO");
                /*
                        $data->expire_time = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $fp = fopen("weixinJS/jsapi_ticket.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
                 * 
                 */
            }
        } else {
            $ticket = $data['jsapi_ticket'];
        }
        return $ticket;
  }

    public function Login(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $res = $this->Data_model->get_single_data(array('name'=>'中兴精密',
                                                            'type'=>'Baogong_51'),"WEIXININFO");
            $this->get_userId($res['access_token'], $code,$res['agentid']);
        }else{
            echo "NO CODE";
        }
    }
    
    function get_userId($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode'])){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014'){
                    $this->get_access_token();
                    
                    /*
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
					$json=self::http_request_json($url);//这个地方不能用file_get_contents
					$data=json_decode($json,true);
					$this->index($data['UserId']);
                     
                     * 
                     */
                    
                    $newurl = "<?=base_url()?>/baogong/getOauth2?code=".$code;
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->getOauth2();
        }
    }
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array('name'=>'中兴精密',
                                                            'type'=>'Baogong_51'),"WEIXININFO");
        if($result['time'] < time()){
            $corpid = $result['corpid'];
            $corpsecret = $result['corpsecret'];
            $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['access_token'])){
                $this->Data_model->update_data(array('name'=>'中兴精密',
                                                                'type'=>'Baogong_51'),array('access_token' => $data['access_token'],'time' => time()+7100),'WEIXININFO');
            
                $access_token = $data['access_token'];
            }else{
                return "获取access_token错误";
            } 
        }else{
            $access_token = $result['access_token'];
        }
        return $access_token;
        
               
    }
    //因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
    function http_request_json($url){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;  
    }
    
    function downloadfile(){
        $mediaid = $_REQUEST['mediaid'];
        $this->Data_model->update_data(array('id' => '4'),array('jsapi_ticket' => $mediaid),'WEIXININFO');
        $access_token = $this->get_access_token();
        $url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$mediaid;
        $fileInfo = $this->downloadWeixinFile($url);
	$savePath = "html/image/";
        $filename = $savePath.date("Ymd",  time())."_".$mediaid.".jpg";
        $this->saveWeixinFile($filename, $fileInfo["body"]);
        echo $filename;
    }
    function downloadWeixinFile($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);    
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package)); 
        return $imageAll;
    }
 
    function saveWeixinFile($filename, $filecontent)
    {
        $local_file = fopen($filename, 'w');
        if (false !== $local_file){
            if (false !== fwrite($local_file, $filecontent)) {
                fclose($local_file);
            }
        }
    }
    /*
     * 通过直接访问数据库获取数据
     */
    public function showMsg(){
        $cardno    = trim($_GET['cardno']);
        $type      = trim($_GET['type']); 
        $username  = trim($_GET['username']);
        $userid    = trim($_GET['userid']);
        //'1511137-4'
        $this->dbConnectsql = $this->load->database("oracle_mes",TRUE);
        $query = $this->dbConnectsql->query("Select T1.FNUMBER 产品代号,T1.FNAME 产品名称,TT.FQty 流转卡数量,t3.fsqty 已汇报量,T1.FDATE 日期,
			FTaskRTNumber 工艺路线 ,t2.foperno 工序号,t2.fopernumber 工序代码,t2.fopername 工站名称,t1.fbillno 派工单号,tt.fflowcardno 流转卡号
                        From zxec.m_flowcard tt 
                        left join zxec.M_Order t1 on tt.ftaskbillno=t1.fbillno 
                        left join zxec.m_orderrt t2 on t1.ftaskrtnumber=t2.frtnumber  AND T1.FBILLNO=T2.FBILLNO
                        left join zxec.m_orderrt t4 on t1.ftaskrtnumber=t4.frtnumber and t4.fopername='出炉检' AND T1.FBILLNO=T4.FBILLNO
                        LEFT JOIN  (select sum(fqty) FSQTY,ftaskoperno,flzk from  zxec.m_taskreport where flzk='过滤条件_流转卡' group by ftaskoperno,flzk) t3 on tt.fflowcardno=t3.flzk and t3.ftaskoperno=t2.foperno
                        Where not t1.fbillno is null  AND nvl(t1.FPQty,0)>0 AND nvl(tt.fstatus,0)=0 AND nvl(t1.fstatus,0)=0 
                        and (( not t4.fid is null and t2.foperno<t4.foperno)  or  t4.fid is null  ) and TT.FQty>nvl(t3.fsqty,0) and tt.fflowcardno = '".$cardno."'
                        ORDER BY 工序号");
        $resDate = $query->result_array();
        $res = array("resDate"     => $resDate,
                        "type"     => $type,
                        "username" => $username,
                        "userid"   => $userid);
        $this->load->view("baogongFinish",$res);
    }
    
    /*
     * 根据流转卡号获取工艺流程数据
     */
    public function GetTaskingByJsonByLZKNO(){
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $LZKNO       = trim($_GET['LZKNO']);
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => "流转卡",
                            "FValue"      => $LZKNO,
                            "FText"       => $LZKNO,);
            foreach ( $return as $key => $value ) { 
                $return[$key] = urlencode ( $value );  
            }
            $parmas = array("strIP"      => "192.168.0.1",
                            "strModule"  => "MES",
                            "strUser"    => $UserName,
                            "strType"    => "WGHBBL",
                            "strEmp"     => $UserNo,
                            "strLZK"     => $LZKNO,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]");
            $return = $client->GetTaskingByJson($parmas);
            $return = $return->GetTaskingByJSONResult;
            $str = str_replace("[[", "[", $return);
            $str = str_replace("\\", "/", $str);
            $arr = json_decode($str,true);
            if(!empty($arr)){
                $res = array("resDate"     => $arr['M_Tasking'],
                        "type"     => "WGHB",
                        "username" => $UserName,
                        "userno"   => $UserNo,
                        "LZNum"    => $arr['M_Tasking'][0]['流转卡数量']);
                $this->load->view("baogongFinish",$res);
            }else{
                $res = array(
                    "type"   => "empty",
                    "resMsg" => "您输入的流转卡号无法找到或者已经删除，请重新输入！",
                    "username" => $UserName,
                    "userno"   => $UserNo
                );
                $this->load->view("baogongFinish",$res);
            }
            
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    /*
     * 根据物料号获取工艺流程
     */
    public function GetTaskingByJsonByMatterNo(){
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $MatterNo    = trim($_GET['MatterNo']);
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array("FTypeNumber" => "WL",     //
                            "FTypeName"   => "物料代码",
                            "FValue"      => $MatterNo,
                            "FText"       => $MatterNo,);
            foreach ( $return as $key => $value ) { 
                $return[$key] = urlencode ( $value );  
            }
            $parmas = array("strIP"      => "192.168.0.1",
                            "strModule"  => "MES",
                            "strUser"    => $UserName,
                            "strType"    => "ZJHBBL",
                            "strEmp"     => $UserNo,
                            "strLZK"     => $MatterNo,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]");
            $return = $client->GetTaskingByJson($parmas);
            $return = $return->GetTaskingByJSONResult;
            
            $str = str_replace("[[", "[", $return);
            $str = str_replace("\\", "/", $str);
            $arr = json_decode($str,true);
            $showid = 10;
            $newArr = array();
            if(is_array($arr['M_Tasking'])){
                foreach ($arr['M_Tasking'] as $key => $value) {
                    $IDArr["ShowId"] = $showid."";
                    $showid = $showid + 10;
                    $newArr[$key] = array_merge($value,$IDArr);
                }
                $res = array("resDate"     => $newArr,
                            "type"     => "ZJHB",
                            "username" => $UserName,
                            "userno"   => $UserNo);
            }else{
                $res = array(
                            "type"     => "empty",
                            'resMsg'   => '您输入的信息无法查询到有效数据，请确认后再查询！',
                            "username" => $UserName,
                            "userno"   => $UserNo);
            }
            
            $this->load->view("baogongFinish",$res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    //非多工序汇报（无不良数量）
    public function TaskSubmitByJSON(){
        header("Access-Control-Allow-Origin:*");
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $LZKNO       = trim($_GET['LZKNO']);
        $TaskOperNo  = trim($_GET['TaskOperNo']);            //工序号
        $TaskBillNo  = trim($_GET['TaskBillNo']);            //派工单号
        $TaskOperNumber = trim($_GET['TaskOperNumber']);     //工序代码
        $TaskOperName   = trim($_GET['TaskOperName']);       //工站名称
        $HBType         = trim($_GET['HBType']);             //报工类型
        $FQty           = trim($_GET['FQty']);               //报工数量
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array(array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => "流转卡",
                            "FValue"      => $LZKNO,
                            "FText"       => $LZKNO,),
                            array("FTypeNumber" => "EMP",
                                "FTypeName"   => "",
                                "Fvalue"      => substr($UserNo, 1),
                                "FText"       => $UserName));
            $parmas = array("strIP"           => "192.168.0.1",
                            "strModule"       => "MES",
                            "strUser"         => $UserName,
                            "strEmp"          => $UserNo,
                            "strLZK"          => $LZKNO,
                            "strTaskBillNo"   => $TaskBillNo,
                            "TeamNumber" => $UserNo,
                            "TeamName" => $UserName,
                            "Qty"   => $FQty,
                            "ProdWeight" => "0",
                            "Weight" => "0",
                            "TaskOpenNo" => $TaskOperNo,
                            "TaskOperNumber" => $TaskOperNumber,
                            "TaskOperName" => $TaskOperName,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]",
                            "strFuncNumber"   => $HBType);
            $resArr = $client->TaskSubmitByJSON($parmas)->TaskSubmitByJSONResult->string;
            //$resArr = $return->TaskSubmitByJSONResult->string;
            $this->dbConnectsql = $this->load->database("oracle_mes",TRUE);
            $sql = "INSERT INTO WEIXINBGLOG (FLZK,FTEAMNUMBER,FTEAMNAME,FDATE,FQTY,FTASKOPERNO,FTASKOPERNUMBER,FTASKOPERNAME) VALUES ('$LZKNO','$UserNo','$UserName','".  date("Y-m-d H:i:s")."','$FQty','$TaskOperNo','$TaskOperNumber','$TaskOperName')";
            $query = $this->dbConnectsql->query($sql);
            if($resArr[0] == "成功"){
                $type = "success";
                $resMsg = "报工数量写入成功！";
            }else{
                $type = "faild";
                $resMsg = $resArr[1];
            }
            $res = array("type"     => $type,
                        "resMsg"   => $resMsg,
                        "username" => $UserName,
                        "userno"   => $UserNo);
            echo json_encode($res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    /*
     * 多工序报工(无不良数量)
     */
    public function TaskSubmitMultiByJSON(){
        header("Access-Control-Allow-Origin:*");
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $LZKNO       = trim($_GET['LZKNO']);
        $json_TaskOperNo  = trim($_GET['json_TaskOperNo']);            //工序号
        $TaskBillNo  = trim($_GET['TaskBillNo']);            //派工单号
        $json_TaskOperNumber = trim($_GET['json_TaskOperNumber']);     //工序代码
        $json_TaskOperName = trim($_GET['json_TaskOperName']); //工站名称
        //$TaskOperName   = trim($_GET['TaskOperName']);       //工站名称
        $HBType         = trim($_GET['HBType']);             //报工类型
        $FQty           = trim($_GET['FQty']);               //报工数量
        
        $TaskOperNoArr = json_decode($json_TaskOperNo);
        $TaskOperNumberArr = json_decode($json_TaskOperNumber);
        $TaskOperNameArr = json_decode($json_TaskOperName);
        $selectArr = array();
        foreach ($TaskOperNoArr as $key => $value) {
            $selectArr[$key]['工序号'] = $value;
        }
        foreach ($TaskOperNumberArr as $key => $value) {
            $selectArr[$key]['工序代码'] = $value;
        }
        foreach ($TaskOperNameArr as $key => $value) {
            $selectArr[$key]['工站名称'] = $value;
        }
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array(array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => "流转卡",
                            "FValue"      => $LZKNO,
                            "FText"       => $LZKNO,),
                            array("FTypeNumber" => "EMP",
                                "FTypeName"   => "",
                                "Fvalue"      => substr($UserNo, 1),
                                "FText"       => $UserName));
                        
                        
            foreach($selectArr as $key => $value){
                foreach ($value as $k => $v){
                    $selectArr[$key][$k] = urlencode($v);
                }
            }
            $strSelectJson = "[".urldecode(json_encode($selectArr))."]";
            $strSelectJson = str_replace('\u5de5\u5e8f\u53f7', '工序号', $strSelectJson);
            $strSelectJson = str_replace('\u5de5\u5e8f\u4ee3\u7801', '工序代码', $strSelectJson);
            $strSelectJson = str_replace('\u5de5\u7ad9\u540d\u79f0', '工站名称', $strSelectJson);
            $parmas = array("strIP"           => "192.168.0.1",
                            "strModule"       => "MES",
                            "strUser"         => $UserName,
                            "strEmp"          => $UserNo,
                            "strLZK"          => $LZKNO,
                            "strTaskBillNo"   => $TaskBillNo,
                            "TeamNumber" => $UserNo,
                            "TeamName" => $UserName,
                            "Qty"   => $FQty,
                            "ProdWeight" => "0",
                            "Weight" => "0",
                            "TaskOpenNo" => '',
                            "TaskOperNumber" => '',
                            "TaskOperName" => '',
                            "strSelectJson"   => $strSelectJson,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]",
                            "strFuncNumber"   => $HBType);
            $resArr = $client->TaskSubmitMultiByJSON($parmas)->TaskSubmitMultiByJSONResult->string;
            //var_dump($resArr);exit;
            //$resArr = $client->TaskSubmitByJSON($parmas)->TaskSubmitMultiByJSON->string;
            //$resArr = $return->TaskSubmitByJSONResult->string;
            //$this->dbConnectsql = $this->load->database("oracle_mes",TRUE);
            //$sql = "INSERT INTO WEIXINBGLOG (FLZK,FTEAMNUMBER,FTEAMNAME,FDATE,FQTY,FTASKOPERNO,FTASKOPERNUMBER,FTASKOPERNAME) VALUES ('$LZKNO','$UserNo','$UserName','".  date("Y-m-d H:i:s")."','$FQty','$TaskOperNo','$TaskOperNumber','$TaskOperName')";
            // $query = $this->dbConnectsql->query($sql);
            if($resArr[0] == "成功"){
                $type = "success";
                $resMsg = "报工数量写入成功！";
            }else{
                $type = "faild";
                $resMsg = $resArr[1];
            }
            $res = array("type"     => $type,
                        "resMsg"   => $resMsg,
                        "username" => $UserName,
                        "userno"   => $UserNo);
            echo json_encode($res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    
    /*
     * 多工序报工(有不良数量)完工汇报
     */
    public function TaskQSSubmitByJSONMobile(){
        header("Access-Control-Allow-Origin:*");
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $LZKNO       = trim($_GET['LZKNO']);
        $json_TaskOperNo  = trim($_GET['json_TaskOperNo']);            //工序号
        $TaskBillNo  = trim($_GET['TaskBillNo']);            //派工单号
        $json_TaskOperNumber = trim($_GET['json_TaskOperNumber']);     //工序代码
        $json_TaskOperName = trim($_GET['json_TaskOperName']); //工站名称
        //$TaskOperName   = trim($_GET['TaskOperName']);       //工站名称
        $HBType         = trim($_GET['HBType']);             //报工类型
        $FQty           = trim($_GET['FQty']);               //良品报工数量
        $BadNum         = trim($_GET['BadNum']);             //不良报公数量
        if($FQty == ""){
            $FQty = "0";
        }
        
        $badreasonjsonhtml = trim($_GET['badreasonjsonhtml']);//不良报公json字符串
        
        
        $TaskOperNoArr = json_decode($json_TaskOperNo);
        $TaskOperNumberArr = json_decode($json_TaskOperNumber);
        $TaskOperNameArr = json_decode($json_TaskOperName);
        
        $selectArr = array();
        foreach ($TaskOperNoArr as $key => $value) {
            $selectArr[$key]['工序号'] = $value;
        }
        foreach ($TaskOperNumberArr as $key => $value) {
            $selectArr[$key]['工序代码'] = $value;
        }
        foreach ($TaskOperNameArr as $key => $value) {
            $selectArr[$key]['工站名称'] = $value;
        }
        $ProduNo   = $selectArr[0]['工序号'];
        $ProduCode = $selectArr[0]['工序代码'];
        $WorkName  = $selectArr[0]['工站名称'];
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array(array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => urlencode("流转卡"),
                            "FValue"      => $LZKNO,
                            "FText"       => $LZKNO,),
                            array("FTypeNumber" => "EMP",
                                "FTypeName"   => "",
                                "Fvalue"      => substr($UserNo, 1),
                                "FText"       => urlencode($UserName)));
                        
                        
            foreach($selectArr as $key => $value){
                foreach ($value as $k => $v){
                    $selectArr[$key][$k] = urlencode($v);
                }
            }
            $strSelectJson = "[".urldecode(json_encode($selectArr))."]";
            $strSelectJson = str_replace('\u5de5\u5e8f\u53f7', '工序号', $strSelectJson);
            $strSelectJson = str_replace('\u5de5\u5e8f\u4ee3\u7801', '工序代码', $strSelectJson);
            $strSelectJson = str_replace('\u5de5\u7ad9\u540d\u79f0', '工站名称', $strSelectJson);
            $parmas = array("strIP"           => "192.168.0.1",
                            "strModule"       => "MES",
                            "strUser"         => $UserName,
                            "strEmp"          => $UserNo,
                            "strLZK"          => $LZKNO,
                            "strTaskBillNo"   => $TaskBillNo,
                /*
                            "TeamNumber" => $UserNo,                //原先
                            "TeamName" => $UserName,                //原先
                            "Qty"   => $FQty,                       //原先
                            "ProdWeight" => "0",                    //原先
                            "Weight" => "0",                        //原先
                            "TaskOpenNo" => '',                     //原先
                            "TaskOperNumber" => '',                 //原先
                            "TaskOperName" => '',                   //原先
                 * 
                 */
                            "Num"             => $FQty,      //new 良品报公数量
                            "BadNum"          => $BadNum,   //new 不良报公数量
                            "ProduNo"         => $ProduNo,  //new 第一行工序号
                            "ProduCode"       => $ProduCode,//new 第一行工序代码
                            "WorkName"        => $WorkName, //new 第一行工站名称
                            "strQSJson"       => str_replace("'", '"', $badreasonjsonhtml), //new 不良报公数量
                            "strSelectJson"   => $strSelectJson,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]",
                            "strFuncNumber"   => $HBType);
            //echo json_encode($parmas);exit;
            $resArr = $client->TaskQSSubmitByJSONMobile($parmas)->TaskQSSubmitByJSONMobileResult->string;
            //$resArr = $client->TaskSubmitByJSON($parmas)->TaskSubmitMultiByJSON->string;
            //$resArr = $return->TaskSubmitByJSONResult->string;
            //$this->dbConnectsql = $this->load->database("oracle_mes",TRUE);
            //$sql = "INSERT INTO WEIXINBGLOG (FLZK,FTEAMNUMBER,FTEAMNAME,FDATE,FQTY,FTASKOPERNO,FTASKOPERNUMBER,FTASKOPERNAME) VALUES ('$LZKNO','$UserNo','$UserName','".  date("Y-m-d H:i:s")."','$FQty','$TaskOperNo','$TaskOperNumber','$TaskOperName')";
            // $query = $this->dbConnectsql->query($sql);
            if($resArr[0] == "成功"){
                $type = "success";
                $resMsg = "报工数量写入成功！";
            }else{
                $type = "faild";
                $resMsg = $resArr[1];
            }
            $res = array("type"     => $type,
                        "resMsg"   => $resMsg,
                        "username" => $UserName,
                        "userno"   => $UserNo);
            echo json_encode($res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    
    //质检汇报最新接口
    public function TaskQSSubmitByJSONMobile_zhijian(){
        header("Access-Control-Allow-Origin:*");
        $UserName    = trim($_GET['UserName']);
        $UserNo      = trim($_GET['UserNo']);
        $LZKNO       = trim($_GET['LZKNO']);
        $TaskOperNo  = trim($_GET['TaskOperNo']);            //工序号
        $TaskBillNo  = trim($_GET['TaskBillNo']);            //派工单号
        $TaskOperNumber = trim($_GET['TaskOperNumber']);     //工序代码
        $TaskOperName = trim($_GET['TaskOperName']); //工站名称
        //$TaskOperName   = trim($_GET['TaskOperName']);       //工站名称
        $HBType         = trim($_GET['HBType']);             //报工类型
        $FQty           = trim($_GET['FQty']);               //良品报工数量
        $BadNum         = trim($_GET['BadNum']);             //不良报公数量
        $badreasonjsonhtml = trim($_GET['badreasonjsonhtml']);//不良报公json字符串
        //echo json_encode($TaskOperNo);exit;
        $ProduNo   = $TaskOperNo;
        $ProduCode = $TaskOperNumber;
        $WorkName  = $TaskOperName;
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $return = array(array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => urlencode("流转卡"),
                            "FValue"      => $LZKNO,
                            "FText"       => $LZKNO,),
                            array("FTypeNumber" => "EMP",
                                "FTypeName"   => "",
                                "Fvalue"      => substr($UserNo, 1),
                                "FText"       => urlencode($UserName)));
            $strSelectJson = '[[{"工序号":"'.$TaskOperNo.'","工序代码":"'.$TaskOperNumber.'","工站名称":"'.$TaskOperName.'"}]]';
            $parmas = array("strIP"           => "192.168.0.1",
                            "strModule"       => "MES",
                            "strUser"         => $UserName,
                            "strEmp"          => $UserNo,
                            "strLZK"          => $LZKNO,
                            "strTaskBillNo"   => $TaskBillNo,
                            "Num"             => $FQty,      //new 良品报公数量
                            "BadNum"          => $BadNum,   //new 不良报公数量
                            "ProduNo"         => $ProduNo,  //new 第一行工序号
                            "ProduCode"       => $ProduCode,//new 第一行工序代码
                            "WorkName"        => $WorkName, //new 第一行工站名称
                            "strQSJson"       => str_replace("'", '"', $badreasonjsonhtml), //new 不良报公数量
                            "strSelectJson"   => $strSelectJson,
                            "strChkJson"      => "[".urldecode(json_encode($return))."]",
                            "strFuncNumber"   => $HBType);
            //echo json_encode($parmas);exit;
            $resArr = $client->TaskQSSubmitByJSONMobile($parmas)->TaskQSSubmitByJSONMobileResult->string;
            if($resArr[0] == "成功"){
                $type = "success";
                $resMsg = "报工数量写入成功！";
            }else{
                $type = "faild";
                $resMsg = $resArr[1];
            }
            $res = array("type"     => $type,
                        "resMsg"   => $resMsg,
                        "username" => $UserName,
                        "userno"   => $UserNo);
            echo json_encode($res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }

    public function result(){
        $type       = $_REQUEST['type'];
        $resMsg     = $_REQUEST['resMsg'];
        $username   = $_REQUEST['username'];
        $userno     = $_REQUEST['userno'];
        $res = array("type"     => $type,
                        "resMsg"   => $resMsg,
                        "username" => $username,
                        "userno"   => $userno);
        $this->load->view("baogongFinish",$res);
    }
    public function test(){
        $a = array('工序号'    => '10',
                    '工序代码' => '1001');
        $b = array('工序号'    => '20',
                    '工序代码' => '1002');
        $arr = array('0'    => $a,
                        '1' => $b);
        echo json_encode($arr);
                
    }
}

